package org.budo.netty.servlet.server.handler.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Locale;

import org.budo.netty.servlet.util.StreamUtil;

/**
 * @author lmw
 */
public class BudoPrintWriter extends PrintWriter {
    private OutputStream outputStream;

    public BudoPrintWriter(OutputStream outputStream) {
        super(outputStream);
        this.outputStream = outputStream;
    }

    @Override
    public void write(char[] buf, int off, int len) {
        byte[] bytes = StreamUtil.charsToBytes(buf);
        this.writeBytes(bytes, off, len);
    }

    @Override
    public void write(String str, int off, int len) {
        byte[] bytes = str.getBytes();
        this.writeBytes(bytes, off, len);
    }

    private void writeBytes(byte[] bytes, int off, int len) {
        try {
            this.outputStream.write(bytes, off, len);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(int c) {
        throw new RuntimeException();
    }

    @Override
    public boolean checkError() {
        throw new RuntimeException();
    }

    @Override
    public void close() {
        throw new RuntimeException();
    }

    @Override
    public void flush() {
        throw new RuntimeException();
    }

    @Override
    public PrintWriter format(Locale locale, String format, Object... args) {
        throw new RuntimeException();
    }

    @Override
    public PrintWriter format(String format, Object... args) {
        throw new RuntimeException();
    }
}
